package com.yjd.pub.auth.service;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import com.yjd.comm.bank.model.BankModel;
import com.yjd.comm.bank.service.IBankService;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.util.FrameUtil;
import org.springframework.stereotype.Service;

import com.yjd.comm.base.dao.IBaseDao;
import com.yjd.comm.base.service.impl.BaseServiceImpl;
import com.yjd.comm.auth.model.LivingBodyAuthModel;
import com.yjd.comm.auth.service.ILivingBodyAuthService;
import com.yjd.pub.auth.dao.ILivingBodyAuthDao;

/**
 * <b>description</b>：活体认证订单表业务实现<br>
 * <b>time</b>：2018-02-08 16:39:28 <br>
 * <b>author</b>：  carl
 */
@Service("livingBodyAuthService")
public class LivingBodyAuthServiceImpl extends BaseServiceImpl implements ILivingBodyAuthService {

	/**
	 * 插入
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public LivingBodyAuthModel insert(LivingBodyAuthModel model) throws Exception {
		return this.livingBodyAuthDao.insertModel(model, true);
	}

	/**
	 * 更新
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public int update(LivingBodyAuthModel model) throws Exception {
		return this.livingBodyAuthDao.updateModel(model, true);
	}

	/**
	 * 根据id删除
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public int delete(long id) throws Exception {
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("id", id);
		return this.livingBodyAuthDao.deleteModel(paramMap, true);
	}

	@Override
	public int updateByMap(Map parm) throws Exception {
		return livingBodyAuthDao.updateByMap(parm,true);
	}

	@Override
	public void authCallback(Long user_id, Integer bus_type, Long id,Integer result) throws Exception {
		if(user_id==null||bus_type==null||id==null||result==null){
			FrameUtil.throwBaseException1("请求参数不能为空");
		}
	   LivingBodyAuthModel bodyAuthModel = livingBodyAuthDao.getModelById(id,true);
		if(bodyAuthModel==null){
			FrameUtil.throwBaseException1("认证信息不存在");
		}

		BankModel bankModel = bankService.getModelById(bodyAuthModel.getUser_id(), DbWREnums.WRITE);
		if(bankModel!=null){
			if(bankModel.getValidate_flag().intValue()==0){
				bankService.updateByMap(FrameUtil.newHashMap("user_id",bodyAuthModel.getUser_id(),"validate_flag",1));
			}
		}
		if(livingBodyAuthDao.updateByMap(FrameUtil.newHashMap("id",id,"curr_result",0,"auth_result",result,"update_time",FrameUtil.getTime()),true)!=1){
			FrameUtil.throwBaseException1("认证检测失败");
		}
	}

	@Resource
	private IBankService bankService;

	@Resource
	private ILivingBodyAuthDao livingBodyAuthDao;

	@Override
	public IBaseDao getBaseDao() {
		return this.livingBodyAuthDao;
	}

}
